package tech.aistar.day11;

/**
 * 本类用来演示: A1aB2bC3c    .....  Z26z
 *
 * @author: success
 * @date: 2022/8/2 8:56 上午
 */
public class PrintLetterAndNumDemo {
    //定义一个标记位
    private int count = 0;

    //wait一定是要在同步代码块中的
    //定义三个方法
    public synchronized void printUpper(){
        for (char i = 'A'; i <= 'Z' ; i++) {
            while(count!=0){
                try {
                    wait(); // ?
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print(i);
            count=1;
            notifyAll();
        }
    }

    public synchronized void printNum(){
        for (int i = 1; i <=26; i++) {
            while(count!=1){
                try {
                    wait(); // ?
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print(i);
            count=2;
            notifyAll();
        }
    }

    public synchronized void printLower(){
        for (char i = 'a'; i <= 'z' ; i++) {
            while(count!=2){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print(i);
            count=0;
            notifyAll();
        }
    }
}
class PrintLetterAndNumDemoTest{
    public static void main(String[] args) {
        PrintLetterAndNumDemo demo = new PrintLetterAndNumDemo();
        //大写
        Thread upper = new Thread(()->demo.printUpper());

        //数字
        Thread num = new Thread(()->demo.printNum());

        //小写
        Thread lower = new Thread(()->demo.printLower());

        upper.start();
        num.start();
        lower.start();
    }
}
